<template>
	<view class="content">
		
		<nuoyu-shoppingcart :main_color="themeColor.main_color" @success="successPlay" @delete="deleteData" :insideCarGoods="list"></nuoyu-shoppingcart>
		<nuoyu-tabbar :backgroundColor="themeColor.tabbarColor" :color="themeColor.font_assist" :selectedColor="themeColor.main_color" :current="current" :tabBar="tabBar"></nuoyu-tabbar>
		<!-- #ifdef MP-WEIXIN -->
			<nuoyu-payment :bottom="'120px'" :stock="0" :type="'单规格'" :main-color="themeColor.main_color" :money="monery" :show="payVisible" @cancel="cancelPayHandle" @success="successPayHandle"></nuoyu-payment>
		<!-- #endif -->
		
		<!-- #ifdef H5 || APP-PLUS || MP-ALIPAY -->
			<nuoyu-payment :bottom="'80px'" :stock="0" :type="'单规格'" :main-color="themeColor.main_color" :money="monery" :show="payVisible" @cancel="cancelPayHandle" @success="successPayHandle"></nuoyu-payment>
		<!-- #endif -->
		
		
		<u-popup bgColor="transparent" :show="noticeShow" mode="center" >
			<view class="popupNotice">
				<view class="popupNoticeTitme">
					<view>
						<image style="width:64rpx" src="../../static/dian.png" mode="widthFix"></image>
					</view>
					<view :style="{'color':themeColor.main_color,'fontSize':'24rpx'}">
						温馨提示!
					</view>
					<view>
						<image style="width:64rpx;transform: scaleX(-1);" src="../../static/dian.png" mode="widthFix"></image>
					</view>
				</view>
				<view class="popupNoticeCotent">
					<view class="u-content">
						<text>您有（{{cotentCount}}）条订单未支付成功,请您保持余额畅通!</text>
					</view>
				</view>
				<view class="popupNoticeFoot">
					<u-button text="关闭" @click="noticeShow=false"></u-button>
					<u-button text="知道啦" @click="noticeShow=false" :color="themeColor.main_color"></u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>

export default {
	  data() {
		return {
			current: 2,
			tabBar:[],
			themeColor:'',
			wechatConfig:'',
			payVisible:false,
			list:[],
			content:'',
			screenHeight:0,
			monery:0.00,
			selectedList:[],
			noticeShow:false,
			cotentCount:0,
		}
	  },
	onLoad() {
		this.tabBar = this.$store.state.tabBarList || uni.getStorageSync('tabbar')
		this.current = this.$store.state.current || 2
		this.themeColor = this.$store.state.themeConfig || uni.getStorageSync('themeConfig')
		this.wechatConfig = this.$store.state.wechatConfig ||  uni.getStorageSync('wechatConfig')
		 
		const info = uni.getSystemInfoSync();
		this.screenHeight = info.screenHeight
		
		this.getShoppingCart();
		// this.getOrderList();
	},
	methods: {
	 
		playMent(){
			this.content = ''
			this.cotentCount = 0
			let that = this
			for (var i = 0; i < this.selectedList.length; i++) {
				let data = {
					goods_id:this.selectedList[i].goods_id,
					wechat_openid:uni.getStorageSync('userInfo').openid,
					wechatphone:uni.getStorageSync('userInfo').phone,
					businessId:this.selectedList[i].businessId,
					paypal_type:3,
					quantity:this.selectedList[i].goods_quantity,
					order_type:'1',
					goodsSku:this.selectedList[i].goods_sku?2:1,
					id:this.selectedList[i].id
				}
				if(this.selectedList[i].goods_sku?2:1 == 2){
					data.money = JSON.parse(this.selectedList[i].goods_sku).price
					data.uuid = JSON.parse(this.selectedList[i].goods_sku).id
				}
				 
				this.tui.request("/basic-capi/wechatApi/order/createOrder", "post",data).then(res=>{
					if(res.code == 200){
						 
						that.buyDeleteData(data)
					}else{
						if(res.message == '请填写您得收货地址再购买!'){
							// this.storeShowAddress = true
							uni.showToast({
								title:"请填写您得收货地址再购买!"
							})
							return
						}
						uni.showToast({
							title:res.message
						})
						this.cotentCount += 1;
						if(i == 0){
							that.content += data.goods_id;
						}else{
							that.content += `,`+ data.goods_id;
						}
						
						setTimeout(()=>{
							that.noticeShow = true
							this.payVisible = false
						},1000)
					}
					
				})
				
				
			}
			
			
			
			
		},
		cancelPayHandle: function() {
			this.payVisible = false;
			this.getShoppingCart();
		},
		successPayHandle(item,stock) {
		 
			// this.quantity = stock;
			if(item=='微信'){
				uni.showToast({
					title:'暂不支持!'
				})
				return
			}
			if(item == '余额'){
				this.playMent();
			}
			
		},
		getShoppingCart(){
			this.tui.request("/basic-capi/shoppingCart/getShoppingCart", "get",{
				pageNum:1,
				pageSize:10,
			}).then((res) => {
				if(res.code == 200){
					this.list = res.data.list
					this.list.forEach((item,index)=>{
						item['selected'] = ''
					})
				}
				
			}).catch((res) => {
				console.log(res)
			})
		},
		buyDeleteData(item){
			this.tui.request("/basic-capi/shoppingCart/deleteShoppingCart", "post",{
				dataList:[{id:item.id}],
			}).then((res) => {
				if(res.code == 200){
					uni.showToast({
						title:`${item.goods_id}订单支付成功!`,
						icon:"success"
					})
					this.getShoppingCart();
				}
				
			}).catch((res) => {
				console.log(res)
			})
		},
		deleteData(item){
			this.tui.request("/basic-capi/shoppingCart/deleteShoppingCart", "post",{
				dataList:item,
			}).then((res) => {
				if(res.code == 200){
					uni.showToast({
						title:"删除成功!",
						icon:"success"
					})
					this.getShoppingCart();
				}
				
			}).catch((res) => {
				console.log(res)
			})
		},
		successPlay(sumPrice,selectedList){
			this.monery = sumPrice;
			this.selectedList = selectedList
			this.payVisible = true;
		}
	}
}
</script>

<style lang="scss" scoped>
	.content{
		
		// grid-auto-rows: 200px;
		// gap: 20rpx;
		// margin: 20rpx;
	 
	}
	.popupNotice{
			// height: 60vh;
			position: relative;
			width:305px;
			border-radius: 8rpx;
			background: white;
			display: flex;
			flex-direction: column;
			z-index:1;
		 
			.popupNoticeTitme{
				height: 80rpx;
				width:100%;
				display: flex;
				justify-content: space-around;
				align-items: center;
				color: #444444;
				font-size: 26rpx;
				font-weight: 600;
				
				// background: red;
			}
			.popupNoticeCotent{
				flex: 1;
				padding:0 16rpx;
				// height: 200rpx;
				
			}
			.popupNoticeFoot{
				// padding:0 0rpx 0 20rpx;
				margin: 26rpx;
				height: 80rpx;
				// position: fixed;
				display: flex;
				gap: 20rpx;
				width:90%;
				bottom: 0px;
			}
			// padding: 20rpx;
	}
	
	.u-content {
	    // padding: 24rpx;
	    // font-size: 32rpx;
	    color: $u-content-color;
	    line-height: 1.6;
		
	  }
</style>
